home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4618 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  74 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Command line Arguments
  5. Date: 6 Feb 1996 04:49:30 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4f6mkq$2f7@news.iag.net>
  8. References: <4f2qev$9jq@cloner3.netcom.com> <4f60cr$34v@jaxnet.jaxnet.com>
  9. NNTP-Posting-Host: pm4-orl9.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4f60cr$34v@jaxnet.jaxnet.com>, garyg@jax.jaxnet.com says...
  13. >
  14. >Glen 'Steve' Vandiver (buxx@ix.netcom.com) wrote:
  15. >: Hi! I have been haveing trouble with my commandline arguements. I have
  16. >: it too where i can read the entire argument string after the run. No
  17. >: prob. but lets say i want it to split it up like this. the first word
  18. >: goes into char *user; and the rest goes into char *command;. HOW WOULD
  19. >: I DO THIS? thanx! bye!
  20. >
  21. >I think this is what you mean:
  22. >
  23. >#include <stdio.h>
  24. >#include <string.h>
  25.  
  26. #include <stdlib.h>  /* for exit */
  27.  
  28. >#define LEN 256
  29. >int main (int argc,char **argv)
  30. >{
  31. >
  32. >        char user[LEN], command[LEN*4];
  33.  
  34. char user[LEN], command[LEN*4] = ""; /* initialization required */
  35.  
  36.  
  37. >        int i;
  38. >        if(argc<3) {
  39. >                printf("Enter more command line args. Bye\n");
  40. >                exit (1);
  41.  
  42. exit( EXIT_FAILURE); /* this is more portable */
  43.  
  44. >        }
  45. >        strcpy(user,argv[1]);
  46. >        for(i=2;i<argc;i++)
  47. >                strcat(command,argv[i]);
  48.  
  49. If you don't initialize command to an empty string before the first strcat,
  50. your results will be a bit unpredictable.
  51.  
  52. I believe that this is os dependent, but I suspect that on most systems
  53. this will cause the args to blur together (ie there will be no spaces
  54. serarating them).  You might want to modify this a bit.  How about:
  55.  
  56. /* remember to add definitions for currEOS and numChrs */
  57.    for(i=2;i<argc;i++)
  58.       {
  59.       /* you may want to add a test here to protect command's bounds */
  60.       sprintf(command + currEOS, "%s %n", argv[i], &numChrs);
  61.       currEOS += numChrs;
  62.       }
  63.  
  64.  
  65. >        printf("User: %s\tCommand: %s\n",user,command);
  66. >        return 0;
  67. >}
  68. >
  69.  
  70. -- 
  71. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  72. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  73.  
  74.